<template>
  <div>
    <el-dialog v-model="dialogVisible" title="Tips" width="50%" :before-close="handleClose">
      <el-form :model="form">
        <el-form-item>
          <Editor v-model="form.content" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">Cancel</el-button>
          <el-button type="primary" @click="dialogVisible = false">Confirm</el-button>
        </span>
      </template>
    </el-dialog>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i>
          文章信息
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-input v-model="query.name" placeholder="文章名称" class="handle-input mr10"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
      >
        <el-table-column prop="articleId" label="文章ID" width="55" align="center"></el-table-column>
        <el-table-column prop="articleName" label="文章名称" width="80"></el-table-column>
        <el-table-column label="文章类型" width="100" align="center">
          <template #default="scope">
            <el-tag
              :type="
                scope.row.type == '0'
                  ? 'danger'
                  : scope.row.type == '1'
                  ? 'success'
                  : 'warning'
              "
            >
              {{
              scope.row.type == "0"
              ? "小说"
              : scope.row.type == "1"
              ? "名著"
              : "传记"
              }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="summary" label="摘要"></el-table-column>
        <el-table-column label="状态" align="center" width="80">
          <template #default="scope">
            <el-tag
              :type="
                scope.row.status == '1'
                  ? 'success'
                  : scope.row.status == '0'
                  ? 'danger'
                  : ''
              "
            >
              {{
              scope.row.status == "1"
              ? "正常"
              : scope.row.status == "0"
              ? "停用"
              : "未知"
              }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="createdAt" label="注册时间"></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { ref, reactive } from "vue";
import { ElMessageBox } from "element-plus";
import request from "../../utils/request";
import Editor from "@/components/Editor.vue";
export default {
  components: {
    Editor,
  },
  setup() {
    const tableData = ref([]);
    const pageTotal = ref(0);
    const dialogVisible = ref(false);
    const form = reactive({
      content: "",
    });
    const query = reactive({
      name: "",
      pageIndex: 1,
      pageSize: 10,
    });

    // 查询操作
    const handleSearch = () => {
      getData();
    };
    // 分页导航
    const handlePageChange = (val) => {};
    // 删除操作
    const handleDelete = (index) => {};
    //修改操作
    const handleEdit = (index) => {
      dialogVisible.value = true;
      console.log(index, dialogVisible);
    };
    const handleClose = (done) => {
      ElMessageBox.confirm("Are you sure to close this dialog?")
        .then(() => {
          done();
        })
        .catch(() => {
          // catch error
        });
    };
    // 获取表格数据
    const getData = () => {
      request
        .request({ url: "/article/getAllArticle", method: "get" })
        .then((res) => {
          console.log(res.data.data);
          tableData.value = res.data.data;
          // pageTotal.value = res.pageTotal || 50
        })
        .catch((e) => {
          console.log(e);
        });
    };
    getData();

    return {
      query,
      pageTotal,
      tableData,
      dialogVisible,
      form,
      getData,
      handleSearch,
      handlePageChange,
      handleDelete,
      handleEdit,
      handleClose,
    };
  },
};
</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.mr10 {
  margin-right: 10px;
}

.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}

.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>